package com.zjdiepu.www.manage.api.dz;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.MiddleAuditStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleAuditService;
import com.zjdiepu.www.ibiz.other.IOtherInterviewConfigService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleAudit;
import com.zjdiepu.www.model.dz.ext.DzLoanMiddleAuditExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.ExceptionHelper;
import com.zjdiepu.www.util.YwExtHelper;

/**
 * Controller层<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月02日 13时29分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DzLoanMiddleAuditController", produces="application/json;charset=UTF-8")
@Api(tags = {""}, produces="application/json;charset=UTF-8")
public class DzLoanMiddleAuditController extends BaseController {
    @Resource
    private IDzLoanMiddleAuditService dzLoanMiddleAuditService;
    
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    @Resource
    private IOtherInterviewConfigService otherInterviewConfigService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;

    /**
     * @author zhangzhengjian <br/>
     * @Title: save  <br/>
     * @Description: 新增 <br/>
     * @param dzLoanMiddleAudit
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保存并提交", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzLoanMiddleAudit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzLoanMiddleAudit.setModifierName(currentUser.getName());
    	dzLoanMiddleAudit.setMiddleAuditStatus(MiddleAuditStatus.YI_TI_JIAO.getCode());
        dzLoanMiddleAuditService.saveUpdateAndDelete(dzLoanMiddleAudit, OperateType.add);
        
        YwBusiness ywBusiness=new YwBusiness();
        ywBusiness.setId(dzLoanMiddleAudit.getBusiId());
        ywBusiness.setTimeContractFull(dzLoanMiddleAudit.getTimeContractFull());
        ywBusinessService.saveUpdateAndDelete(ywBusiness,OperateType.edit);
        return ApiResponse.success(true);
    }
    
    @ApiOperation(value = "新增", response = String.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ApiResponse insert(@Validated(Add.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzLoanMiddleAudit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzLoanMiddleAudit.setModifierName(currentUser.getName());
//    	dzLoanMiddleAudit.setMiddleAuditStatus(MiddleAuditStatus.YI_TI_JIAO.getCode());
        dzLoanMiddleAuditService.saveUpdateAndDelete(dzLoanMiddleAudit, OperateType.add);
        return ApiResponse.success(dzLoanMiddleAudit.getId());
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: remove  <br/>
     * @Description: 删除<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzLoanMiddleAuditService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: disable  <br/>
     * @Description: 禁用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzLoanMiddleAuditService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: enable <br/>
     * @Description: 启用<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzLoanMiddleAuditService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author zhangzhengjian <br/>
     * @Title: modify  <br/>
     * @Description: 修改 <br/>
     * @param dzLoanMiddleAudit
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改", response = String.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzLoanMiddleAudit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzLoanMiddleAudit.setModifierName(currentUser.getName());
    	dzLoanMiddleAuditService.updateByPrimaryKey(dzLoanMiddleAudit);
    	
        return ApiResponse.success(dzLoanMiddleAudit.getId());
    }
    @ApiOperation(value = "根据业务ID获取单条记录", response = DzLoanMiddleAudit.class, httpMethod="POST")
    @RequestMapping(value = "getByBusiId", method = RequestMethod.POST)
    public ApiResponse getByBusiId(String busiId) {
    	getCurrentUser();
    	if(StringUtils.isBlank(busiId)) {
    		ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
    	}
    	DzLoanMiddleAudit dzLoanMiddleAudit = new DzLoanMiddleAudit();
    	dzLoanMiddleAudit.setBusiId(busiId);
    	dzLoanMiddleAudit.setDpStatus(DpStatus.NORMAL.getCode());
    	dzLoanMiddleAudit = dzLoanMiddleAuditService.selectRefined(dzLoanMiddleAudit);
//    	if(null == dzLoanMiddleAudit) {
//    		ExceptionHelper.thow(ResultFlagType.MISSING.getCode().toString(), "贷中信息记录丢失或已被删除");
//    	}
        return ApiResponse.success(dzLoanMiddleAudit);
    }
    @ApiOperation(value = "保存提交时间", response = String.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "saveTime", method = RequestMethod.POST)
    public ApiResponse saveTime(@Validated(Add.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzLoanMiddleAudit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzLoanMiddleAudit.setModifierName(currentUser.getName());
    	dzLoanMiddleAuditService.saveTime(dzLoanMiddleAudit, false);
    	
        return ApiResponse.success(dzLoanMiddleAudit.getId());
    }
    
    @ApiOperation(value = "修改提交时间", response = String.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modifyTime", method = RequestMethod.POST)
    public ApiResponse modifyTime(@Validated(Update.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzLoanMiddleAudit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzLoanMiddleAudit.setModifierName(currentUser.getName());
    	dzLoanMiddleAuditService.modifyTime(dzLoanMiddleAudit);
    	
        return ApiResponse.success(dzLoanMiddleAudit.getId());
    }
    
    @ApiOperation(value = "提交时间", response = String.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "commitTime", method = RequestMethod.POST)
    public ApiResponse commitTime(@Validated(Add.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	String id = dzLoanMiddleAudit.getId();
    	if(StringUtils.isBlank(id)) {
    		dzLoanMiddleAuditService.saveTime(dzLoanMiddleAudit, true);
    	} else {
    		dzLoanMiddleAuditService.modifyTime(dzLoanMiddleAudit);
    	}
    	return ApiResponse.success(dzLoanMiddleAudit.getId());    	
    }
    
    @ApiOperation(value = "修改并提交", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ApiResponse update(@Validated(Update.class) @ModelAttribute DzLoanMiddleAudit dzLoanMiddleAudit) {
    	AbstractSysUser currentUser = getCurrentUser();
        dzLoanMiddleAudit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzLoanMiddleAudit.setModifierName(currentUser.getName());
        dzLoanMiddleAuditService.updateAll(currentUser,dzLoanMiddleAudit);
        return ApiResponse.success(true);
    }
    /**
     * @author zhangzhengjian <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询", response = DzLoanMiddleAudit.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DzLoanMiddleAudit dzLoanMiddleAudit = dzLoanMiddleAuditService.selectById(id);
       if(null == dzLoanMiddleAudit) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dzLoanMiddleAudit);
    }
    
    /**
     * @author zhangzhengjian <br/>
     * @Title: list  <br/>
     * @Description: 条件查询 <br/>
     * @param dzLoanMiddleAuditExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询",response = DzLoanMiddleAudit.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DzLoanMiddleAuditExt dzLoanMiddleAuditExt){
        dzLoanMiddleAuditExt.setBaseWhere();
        PageVo page = getPage(dzLoanMiddleAuditExt);
        int records = dzLoanMiddleAuditService.countByExt(dzLoanMiddleAuditExt);
        page.setRecords(records);
        if (records > 0) {
            List<DzLoanMiddleAudit> dzLoanMiddleAudits = dzLoanMiddleAuditService.selectByExt(dzLoanMiddleAuditExt);
            page.setRows(dzLoanMiddleAudits);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
        ywBusinessQueryRequest.setBusiStatusLess(BusiStatus.CHU_SHEN_DAN_CHU_SHEN_ZHONG.getCode());
        ywBusinessQueryRequest.setBusiStatusMore(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
//        ywBusinessQueryRequest.setTimeApplyOpencardFlag(true);
//        ywBusinessQueryRequest.setTimeContractFullFlag(false);
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
	}
}
